﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BingImages
{
    public class Folder
    {
        private string _wallpaperPath;
        public string wallpaperPath
        {
            get { return _wallpaperPath; }
        }

        private string _tempPath;
        public string tempPath
        {
            get { return _tempPath; }
        }

        private string _themePath;
        public string themePath
        {
            get { return _themePath; }
        }

        private string _last;
        public string last
        {
            get { return _last; }
        }

        public Folder()
        {
            this.createFolders("wallpaper");
            this.createFolders("temp");
            this.createFolders("theme");
            this.createFolders("last");

            this._wallpaperPath = @"%USERPROFILE%\AppData\Local\BingImages\wallpaper";
            this._wallpaperPath = Environment.ExpandEnvironmentVariables(this._wallpaperPath);
            this._tempPath = @"%USERPROFILE%\AppData\Local\BingImages\temp";
            this._tempPath = Environment.ExpandEnvironmentVariables(this._tempPath);
            this._themePath = @"%USERPROFILE%\AppData\Local\BingImages\theme";
            this._themePath = Environment.ExpandEnvironmentVariables(this._themePath);
            this._last = @"%USERPROFILE%\AppData\Local\BingImages\last";
            this._last = Environment.ExpandEnvironmentVariables(this._last);
        }

        public void createFolders(string folderName)
        {
            string path = "";
            string directoryPath = "";

            path = @"%USERPROFILE%\AppData\Local\BingImages\" + folderName;
            directoryPath = Environment.ExpandEnvironmentVariables(path);

            // Création du dossier s'il n'existe pas
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
        }

        public FileInfo[] getFiles(string folderName, string option)
        {
            string path = "";
            string directoryPath = "";
            DirectoryInfo directory = null;
            FileInfo[] files = null;

            path = @"%USERPROFILE%\AppData\Local\BingImages\" + folderName;
            directoryPath = Environment.ExpandEnvironmentVariables(path);
            directory = new DirectoryInfo(directoryPath);
            files = directory.GetFiles(option);

            return files;
        }

        public FileInfo[] getFiles(DirectoryInfo directory, string option)
        {
            FileInfo[] files = null;

            files = directory.GetFiles(option);

            return files;
        }

        public void deleteFiles(string folderName, string type)
        {
            FileInfo[] files = null;
                      
            files = this.getFiles(folderName, type);            

            if (files.Length > 0)
            {
                // Suppression des fichiers
                foreach (FileInfo file in files)
                {
                    file.Delete();
                }
            }
        }

        public void deleteFile(string folderName, string fileToDelete)
        {
            File.Delete(Path.Combine(folderName, fileToDelete));
        }

        public bool exist(string folderName)
        {
            bool result = false;

            if (!string.IsNullOrEmpty(folderName))
            {
                folderName = Environment.ExpandEnvironmentVariables(folderName);
            }            

            if (!string.IsNullOrEmpty(folderName) && Directory.Exists(folderName))
            {
                result = true;
            }

            return result;
        }
    }
}
